import cache from './../../utils/cache'

const state = {
    device: null,
    lock: cache.get(cache.key.LOCK),
    //左边导航页
    sidebar: {
        opened: cache.get(cache.key.SIDEBAR_STATUS) ? !!cache.get(cache.key.SIDEBAR_STATUS) : true,
        withoutAnimation: false
    },
    //顶部tags页，也可以说面包屑
    tagsView: {
        visitedViews: [],
        cachedViews: []
    },

    mainRoutes: null, //左边导航路由列表
    btnPermission: {}, //页面button权限
    keepAlive: {} //不需要缓存的页面
}

const mutations = {
    M_UPDATE_DEVICE(state, device) {
        state.device = device
    },
    M_UPDATE_LOCK(state, lock) {
        state.lock = lock
        cache.set(cache.key.LOCK, lock)
    },
    M_TOGGLE_SIDEBAR(state) {
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = false
        if (state.sidebar.opened) {
            cache.set(cache.key.SIDEBAR_STATUS, 1)
        } else {
            cache.set(cache.key.SIDEBAR_STATUS, 0)
        }
    },
    M_CLOSE_SIDEBAR(state, withoutAnimation) {
        cache.set(cache.key.SIDEBAR_STATUS, 0)
        state.sidebar.opened = false
        state.sidebar.withoutAnimation = withoutAnimation
    },
    M_UPDATE_MAIN_ROUTES(state, routes) {
        state.mainRoutes = routes
    },

    M_UPDATE_BTN_PREMISSION(state, premission) {
        state.btnPermission = premission
    },
    M_CLEAR_ROUTERS(state) {
        state.mainRoutes = null
        state.topRoutes = null
        state.btnPermission = null
        cache.remove(cache.key.ROUTERS)
    },

    //======================
    M_ADD_VISITED_VIEW(state, view) {
        if (state.tagsView.visitedViews.some((v) => v.path === view.path)) return
        state.tagsView.visitedViews.push(
            Object.assign({}, view, {
                title: view.meta.title || 'no-name'
            })
        )
    },

    M_ADD_CACHED_VIEW(state, view) {
        if (state.tagsView.cachedViews.includes(view.name)) return
        if (!view.meta.noCache) {
            state.tagsView.cachedViews.push(view.name)
        }
    },

    M_UPDATE_VISITED_VIEW(state, view) {
        for (let v of state.tagsView.visitedViews) {
            if (v.path === view.path) {
                v = Object.assign(v, view)
                break
            }
        }
    },

    M_DEL_VIEW(state, view) {
        const index = state.tagsView.cachedViews.indexOf(view.name)
        index > -1 && state.tagsView.cachedViews.splice(index, 1)
        for (const [i, v] of state.tagsView.visitedViews.entries()) {
            if (view.name && v.name === view.name) {
                state.tagsView.visitedViews.splice(i, 1)
                break
            } else if (v.path === view.path) {
                state.tagsView.visitedViews.splice(i, 1)
                break
            }
        }
    },

    M_DEL_CACHED_VIEW(state, view) {
        const index = state.tagsView.cachedViews.indexOf(view.name)
        index > -1 && state.tagsView.cachedViews.splice(index, 1)
    },

    M_DEL_ALL_VIEW(state) {
        // keep affix tags
        const affixTags = state.tagsView.visitedViews.filter((tag) => tag.meta.affix)
        state.tagsView.visitedViews = affixTags
        state.tagsView.cachedViews = []
    },

    M_DEAL_OTHER_VIEW(state, view) {
        state.tagsView.visitedViews = state.tagsView.visitedViews.filter((v) => {
            return v.meta.affix || v.path === view.path
        })
        const index = state.tagsView.cachedViews.indexOf(view.name)
        if (index > -1) {
            state.tagsView.cachedViews = state.tagsView.cachedViews.slice(index, index + 1)
        } else {
            // if index = -1, there is no cached tags
            state.tagsView.cachedViews = []
        }
    },

    M_CLEAR_TOKEN(state) {
        state.mainRoutes = null
        state.topRoutes = null
        state.btnPermission = null
        cache.remove(cache.key.ROUTERS)
    },

    M_ADD_KEEP_ALIVE(state, obj) {
        state.keepAlive[obj.fullPath] = obj
    },
    M_REMOVE_KEEP_ALIVE(state, obj) {
        delete state.keepAlive[obj.fullPath]
    },
    M_CLEAR_KEEP_ALIVE(state) {
        state.keepAlive = {}
    }
}

const actions = {
    A_CLEAR_TOKEN(store) {
        store.commit('M_CLEAR_TOKEN')
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
